
var recordCount = 0   //总记录数
var pageSize = 5      //每页显示记录数
var pageCount = 0     //总页数
var pageIndex = 0     //当前页的索引值
var response=null
var fileData=null
function initData() {

    //2、循环遍历数据，组合数据，形成innerHTML字符串
    var strHTML = ''
    for (var i = pageIndex * pageSize; i < (pageIndex + 1) * pageSize; i++) {
        if(fileData[i]){
        strHTML += `<li>
                            <div id="articleImg">
                                <a href="heathChild.html"><img src="${fileData[i].img}" alt=""></a>
                            </div>
                            <div id="articleInfo">
                                <a href="heathChild.html" class="title">${fileData[i].title}</a>
                                <p class="createtime" >${fileData[i].date}</p>
                                <a href="heathChild.html" class="showAll">查看全文</a>
                            </div>
                        </li>`
        }
    }
    $("#article").html(strHTML)
}

response = JSON.parse(localStorage.getItem("mydata"))

window.onload = function () {
     //1、读取文件数据
    $.get('../json/heath.json', function (response) {
        fileData=response
        localStorage.setItem("mydata", JSON.stringify(response))
        initData()
    })
    recordCount = response.length
     pageCount = Math.ceil(recordCount / pageSize)
}
var newfile=null
//点击品牌名称筛选数据
function showInfo(name){
    //循环遍历现有的数据，条件判断品牌名称是否一致
    var strHTML = ''
    fileData=[]
    for (var i = 0; i < response.length; i++) {
   
        //名字相同时，组合数据，拼接innerHTML字符串
        if(response[i].class==name){
           fileData.push(response[i])
        }
        
    }
    console.log(fileData)
     recordCount = fileData.length
     pageIndex = 0 
     pageCount =Math.ceil( recordCount/pageSize) 
     console.log(pageCount)
     initData() 
     setButton(true, true, false, false)
    //赋值DOM节点
    // $("#article").html(strHTML)
 
}

//按钮状态
function setButton(f1, f2, f3, f4) {
    document.getElementById('first').disabled = f1
    document.getElementById('previous').disabled = f2
    document.getElementById('next').disabled = f3
    document.getElementById('last').disabled = f4
}
function first1() {
    pageIndex = 0
    initData()
    setButton(true, true, false, false)
}
function lastOne() {
    pageIndex--
    initData()
    setButton(false, false, false, false)
    if (pageIndex == 0) {
        setButton(true, true, false, false)
    }
}
function nextOne() {
    pageIndex++
    initData()
    setButton(false, false, false, false)
    if(pageIndex==pageCount-1){
        setButton(false, false, true, true)
    }
    
}
function last1() {
    index = pageCount
    pageIndex = pageCount - 1
    initData()
    setButton(false, false, true, true)
}


